package com.guigu.pd.oms.controller;

import java.util.*;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.guigu.common.utils.PageVo;
import com.guigu.pd.serviceapi.authfeign.SsqFeign;
import com.guigu.pd.serviceapi.pojo.OrderEntity;
import com.guigu.pd.serviceapi.workfeign.TransportOrderFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.guigu.pd.oms.service.OrderService;
import com.guigu.common.utils.PageUtils;
import com.guigu.common.utils.R;



/**
 * 订单
 *
 * @author deyran
 * @email deyran@gmail.com
 * @date 2022-07-19 19:29:39
 */
@RestController
@RequestMapping("oms/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private TransportOrderFeign transportOrderFeign;

    @Autowired
    SsqFeign ssqFeign;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageVo<OrderEntity> page = orderService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") String id){
		OrderEntity order = orderService.getorderbyId(id);

        return R.ok().put("order", order).put("ordercargo",order.getOrdercargo()).put("taskpd",order.getTaskpt());
    }

    @GetMapping("/getsendorderbyid/{id}")
    public String getsendorderbyId(@PathVariable("id") String id){
        OrderEntity order = orderService.getById(id);
        String orderstr =order.getSenderName()+","+order.getSenderPhone()+","+order.getSenderAddress();

        return orderstr;
    }

    @GetMapping("/getreceivorderbyid/{id}")
    public String getreceivorderbyId(@PathVariable("id") String id){
        OrderEntity order = orderService.getById(id);
        String orderstr =order.getReceiverName()+","+order.getReceiverPhone()+","+order.getReceiverAddress();

        return orderstr;
    }

    @GetMapping("/getordertimebyid/{id}")
    public Date getordertimebyId(@PathVariable("id") String id){
        OrderEntity order = orderService.getById(id);;

        return order.getCreateTime();
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody OrderEntity order){
		orderService.save(order);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody OrderEntity order){
		orderService.updateById(order);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody String[] ids){
		orderService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }


    //添加订单(客户下单寄件)           Map<String, Object> params
    @RequestMapping("/kuhuxiadanjijian")
    public R kuhuxiadanjijian(@RequestParam Map<String, Object> params){

        R kuhuxiadanjijian = orderService.kuhuxiadanjijian(params);
        return kuhuxiadanjijian;
    }


    @GetMapping("/getorderbyid/{id}")
    public String getroderbyId(@PathVariable("id") String id){
        OrderEntity order = orderService.getById(id);
        String orderstr =
                order.getCreateTime()+","+order.getSenderName()+","+order.getSenderPhone()+","+order.getSenderAddress()+","+order.getReceiverName()+","+order.getReceiverPhone()+","+order.getReceiverAddress();

        return orderstr;
    }

    @GetMapping("/getbysendname/{sname}")
    public String getbySendname(@PathVariable("sname") String sname){
        QueryWrapper<OrderEntity> orderQuery = new QueryWrapper<OrderEntity>();
        orderQuery.eq("sender_name", sname);
        OrderEntity order = orderService.getOne(orderQuery);
        if (order!=null){
            return order.getId();
        }else {
            return "";
        }
    }

    @GetMapping("/getbysendaddress/{saddress}")
    public String getbySendaddress(@PathVariable("saddress") String saddress){
        QueryWrapper<OrderEntity> orderQuery = new QueryWrapper<OrderEntity>();
        orderQuery.eq("sender_address", saddress);
        OrderEntity order = orderService.getOne(orderQuery);
        if (order!=null){
            return order.getId();
        }else {
            return "";
        }
    }

    @GetMapping("/getbyreceivername/{rname}")
    public String getbyReceivername(@PathVariable("rname") String rname){
        QueryWrapper<OrderEntity> orderQuery = new QueryWrapper<OrderEntity>();
        orderQuery.eq("receiver_name", rname);
        OrderEntity order = orderService.getOne(orderQuery);
        if (order!=null){
            return order.getId();
        }else {
            return "";
        }
    }

    @GetMapping("/getbyreceiveraddress/{raddress}")
    public String getbyReceiveraddress(@PathVariable("raddress") String raddress){
        QueryWrapper<OrderEntity> orderQuery = new QueryWrapper<OrderEntity>();
        orderQuery.eq("receiver_address", raddress);
        OrderEntity order = orderService.getOne(orderQuery);
        if (order!=null){
            return order.getId();
        }else {
            return "";
        }
    }

    @GetMapping("/gettimebyid/{orderid}")
    public Date gettimebyId(@PathVariable("orderid")String orderid){
        OrderEntity order = orderService.getById(orderid);
        return order.getCreateTime();
    }

    @GetMapping("/getbysendphone/{sphone}")
    public String getbySendphone(@PathVariable("sphone") String sphone){
        QueryWrapper<OrderEntity> orderQuery = new QueryWrapper<OrderEntity>();
        orderQuery.eq("sender_phone", sphone);
        OrderEntity order = orderService.getOne(orderQuery);
        if (order!=null){
            return order.getId();
        }else {
            return "";
        }
    }

    @GetMapping("/getbyreceiverphone/{rphone}")
    public String getbyReceiverphone(@PathVariable("rphone") String rphone){
        QueryWrapper<OrderEntity> orderQuery = new QueryWrapper<OrderEntity>();
        orderQuery.eq("receiver_phone", rphone);
        OrderEntity order = orderService.getOne(orderQuery);
        if (order!=null){
            return order.getId();
        }else {
            return "";
        }
    }

    @GetMapping("/getorderinfobyid/{id}")
    public OrderEntity getroderinfobyId(@PathVariable("id") String id){
        OrderEntity order = orderService.getById(id);
        return order;
    }

    @GetMapping("/dopickup/{id}")
    public R dopickup(@PathVariable("id") String id){
        UpdateWrapper<OrderEntity> updateWrapper = new UpdateWrapper<OrderEntity>();
        updateWrapper.eq("id", id).set("status",3);
        boolean update = orderService.update(updateWrapper);
        if (update){
            Boolean aBoolean = transportOrderFeign.addtransportorder(id);
            if (aBoolean) {
                return R.ok();
            }else {
                return R.error();
            }
        }else {
            return R.error();
        }
    }

    @GetMapping("/getorderprovince")
    public R getorderProvince(){
        QueryWrapper<OrderEntity> orderQuery = new QueryWrapper<OrderEntity>();
        orderQuery.select("DISTINCT receiver_province_id");
        List<OrderEntity> list = orderService.list(orderQuery);
        List<String> provinces = new ArrayList<String>();

        QueryWrapper<OrderEntity> orderQuery2 = new QueryWrapper<OrderEntity>();
        orderQuery2.select("count(*) as count,receiver_province_id").groupBy("receiver_province_id");
        List<Map<String, Object>> maps = orderService.listMaps(orderQuery2);
        List<Integer> sum = new ArrayList<Integer>();
        for (Map<String, Object> map : maps) {
            for (Map.Entry<String, Object> e : map.entrySet()) {
                String value = e.getValue().toString();
                String key = e.getKey().toString();
                if (key.equals("count")) {
                    sum.add(Integer.parseInt(value));
                }
                if (key.equals("receiver_province_id")) {
                    String s = ssqFeign.getnamebyId(Integer.parseInt(value));
                    provinces.add(s);
                }
            }
        }

        System.out.println(sum);
        return R.ok().put("province",provinces).put("sum",sum);
    }

    //修改订单状态为4
    @GetMapping("/updatestatus/{id}")
    boolean updatestatus(@PathVariable("id") String id){
        LambdaUpdateWrapper<OrderEntity> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(OrderEntity::getId,id).set(OrderEntity::getStatus,4);
        boolean update = orderService.update(lambdaUpdateWrapper);
        return update;
    }


    //先根据机构id 和 状态 3已揽件 获取订单表数据
    @GetMapping("/getorderxxbycurrentAgencyId/{id}")
    public List<OrderEntity> getorderxxbycurrentAgencyId(@PathVariable("id") String id){

        LambdaQueryWrapper<OrderEntity> orderEntityLambdaQueryWrapper = new LambdaQueryWrapper<OrderEntity>();
        orderEntityLambdaQueryWrapper.eq(OrderEntity::getCurrentAgencyId,id).eq(OrderEntity::getStatus,3);

        List<OrderEntity> list = orderService.list(orderEntityLambdaQueryWrapper);


        return list;
    }
}
